﻿'ayoub
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Imports System.Drawing.Imaging

Public Class Frm_Gestion_Equipements
    Dim position As Integer = -1
    
    Dim isBtnSens As Boolean = False
    Dim nouvVider As Boolean = False
    Public Sub vider(ByVal ajout As Boolean)
        For Each txt In Me.Controls
            If TypeOf txt Is TextBox Then
                CType(txt, TextBox).Clear()
            End If
        Next

        If ajout Then
            cmbCategorie.Text = ""
            cmbCategorie.SelectedIndex = -1
            objEquipement = Nothing
            position = -1
        End If

        cmbType.Text = ""
        cmbType.SelectedIndex = -1
        cmbType.Items.Clear()
        objTypEquip = Nothing
        position = 0
        Pic_photo.Image = My.Resources.photo_Equi
    End Sub

    Private Sub Frm_Gestion_Equipements_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        cnClose()
    End Sub
    Private Sub Frm_Gestion_Equipements_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            open()
            remplirEquip()
            For Each equi In lstEquipement
                cmbCategorie.Items.Add(equi.categorie)
            Next
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation)
        End Try

    End Sub
    Private Sub btn_nouv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_nouv.Click
        nouvVider = True
        vider(nouvVider)
        nouvVider = False
    End Sub
    Private Sub btn_parcour_photo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_parcour_photo.Click
        With OpenF
            .FileName = ""
            .Title = "Ovrir la Photo"
            .RestoreDirectory = False
            .InitialDirectory = "C:\"
            .Filter = "JPEG Files (*.JPG)|*.JPG"
            .FilterIndex = 1
        End With
        If OpenF.ShowDialog = Windows.Forms.DialogResult.OK Then
            Pic_photo.Image = Image.FromFile(OpenF.FileName)
        End If
    End Sub
    Private Sub btn_supp_phot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_supp_phot.Click
        Pic_photo.Image = My.Resources.photo_Equi
    End Sub
    Private Sub btn_ajouter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_ajouter.Click
        If (Not String.IsNullOrEmpty(cmbCategorie.Text) And Not String.IsNullOrWhiteSpace(cmbCategorie.Text) _
            And Not String.IsNullOrEmpty(txt_prix.Text) And Not String.IsNullOrEmpty(txtQuantite.Text)) Then
            Try
                open()
                objEquipement = lstEquipement.Where(Function(y As equipement) y.categorie = cmbCategorie.Text)(0)

                If objEquipement Is Nothing Then
                    ajouterEquipement(New equipement With {.categorie = cmbCategorie.Text})
                    objEquipement = lstEquipement.Last(Function(y As equipement) y.idEquip)
                    cmbCategorie.Items.Add(objEquipement.categorie)
                End If
                objEquipement.mesTypes()

                Dim typeEquipement As String = cmbCategorie.Text 'où cas dont on n'aura pas de type
                If Not String.IsNullOrEmpty(cmbType.Text) And Not String.IsNullOrWhiteSpace(cmbType.Text) Then
                    objTypEquip = objEquipement.types.Where(Function(y As typeEquipement) y.libelle = cmbType.Text)(0)
                    typeEquipement = cmbType.Text
                End If

                If objTypEquip Is Nothing Then
                    objEquipement.ajouterType(New typeEquipement With {.libelle = typeEquipement, .prixLoc = txt_prix.Text, .remarque = txt_remarq.Text, _
                                                                  .quantStock = txtQuantite.Text, .Photo = convertPicToByte(Pic_photo.Image)})
                Else
                    MsgBox("Ce type existe déjà!!!", MsgBoxStyle.Critical, "Erreur !")
                    'objEquipement = Nothing
                    Exit Sub
                End If


                MsgBox(" Ajout effectué avec succés", MsgBoxStyle.Information, "Réussi !")
                'vider()
                cmbType.Items.Add(objEquipement.types.Last(Function(y As typeEquipement) y.idType))
                If cmbCategorie.SelectedItem = objEquipement.categorie Then
                    cmbCategorie.SelectedIndex = -1
                    cmbCategorie.SelectedIndex = 0
                Else
                    cmbCategorie.SelectedItem = objEquipement.categorie
                End If

                position = 0
                btnNext_Click(sender, e)
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Exclamation)
            End Try
        Else
            MsgBox("Veuillez remplir tout les champs avec *", MsgBoxStyle.Critical, "Erreur !")
        End If
    End Sub
    Private Sub btnModif_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModif.Click
        If (Not String.IsNullOrEmpty(cmbCategorie.Text) And Not String.IsNullOrWhiteSpace(cmbCategorie.Text) _
            And Not String.IsNullOrEmpty(txt_prix.Text) And Not String.IsNullOrEmpty(txtQuantite.Text)) Then
            Try

                Dim equipFound As Integer = lstEquipement.Where(Function(y As equipement) y.categorie = cmbCategorie.Text).Count
                If equipFound >= 2 Then
                    MsgBox("La catégorie exist déjà!!!", MsgBoxStyle.Critical, "Erreur !")
                ElseIf equipFound = 0 Then
                    Cmd = New SqlCommand("update trEquipement set categorie =@categorie where id =" & objEquipement.idEquip, Con)
                    Cmd.Parameters.AddWithValue("@categorie", cmbCategorie.Text)
                    objEquipement.categorie = cmbCategorie.Text
                    Cmd.ExecuteNonQuery()
                End If

                Dim typeEquipement As String = cmbCategorie.Text 'où cas dont on n'aura pas de type
                If Not String.IsNullOrEmpty(cmbType.Text) And Not String.IsNullOrWhiteSpace(cmbType.Text) Then
                    equipFound = objEquipement.types.Where(Function(y As typeEquipement) y.libelle = cmbType.Text).Count
                    If equipFound >= 2 Then
                        If Not isCmbCatTextChanged Then
                            MsgBox("Ce type existe déjà!!!", MsgBoxStyle.Critical, "Erreur !")
                        End If
                    ElseIf equipFound <= 1 Then
                        Cmd = New SqlCommand("update trTypeEquipement set libelle =@libelle,prixLocation=@prixLocation,quantiteStock=" & txtQuantite.Text & ",remarque=@remarque" _
                                             & ",photo=@photo where idType =" & objTypEquip.idType, Con)
                        Cmd.Parameters.Clear()
                        Cmd.Parameters.AddWithValue("@libelle", cmbType.Text)
                        Cmd.Parameters.AddWithValue("@prixLocation", CDbl(txt_prix.Text))
                        Cmd.Parameters.AddWithValue("@remarque", txt_remarq.Text)
                        Cmd.Parameters.Add("@Photo", SqlDbType.VarBinary).Value = convertPicToByte(Pic_photo.Image)
                        Cmd.ExecuteNonQuery()

                        With objTypEquip
                            .libelle = cmbType.Text
                            .prixLoc = txt_prix.Text
                            .quantStock = CInt(txtQuantite.Text)
                            .remarque = txt_remarq.Text
                            .Photo = convertPicToByte(Pic_photo.Image)
                        End With
                    End If
                End If
                MsgBox("Modification effectué avec succés", MsgBoxStyle.Information, "Réussi !")
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End If
    End Sub
    Private Sub btn_supp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_supp.Click

        If cmbCategorie.SelectedIndex >= 0 And cmbType.SelectedIndex >= 0 Then
            If MsgBox("Voulez Vous Vraiment Supprimer !!!", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "Suppression") = MsgBoxResult.Yes Then
                Try
                    open()
                    objEquipement.supprimerType(objTypEquip.idType)
                    If objEquipement.types.Count <= 0 Then
                        supprimerEquip(objEquipement.idEquip)
                        cmbCategorie.Items.Remove(objEquipement.categorie)
                        nouvVider = True
                        vider(nouvVider)
                        nouvVider = False
                    Else
                        vider(nouvVider)
                        'cmbCategorie.SelectedIndex = -1
                    End If
                    MsgBox("Suppresion effectué !", MsgBoxStyle.Information, "Terminer")
                Catch ex As Exception
                    MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                End Try
            End If
        Else
            MsgBox("Vous n'avez choisi aucun équipement !", MsgBoxStyle.Critical, "Réssayer !")
        End If


    End Sub
    Private Sub txt_prix_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_prix.KeyPress
        Dim allowedChars As String = "1234567890,"
        If txt_prix.Text.Contains(",") Then
            allowedChars = "1234567890"
        End If
        If e.KeyChar = ChrW(8) Then
            e.Handled = False
        ElseIf allowedChars.IndexOf(e.KeyChar) = -1 Then
            e.Handled = True
        End If
    End Sub
    Private Sub txtQuantite_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtQuantite.KeyPress
        Dim allowedChars As String = "1234567890,"
        If txtQuantite.Text.Contains(",") Then
            allowedChars = "1234567890"
        End If
        If e.KeyChar = ChrW(8) Then
            e.Handled = False
        ElseIf allowedChars.IndexOf(e.KeyChar) = -1 Then
            e.Handled = True
        End If
    End Sub
    Private Sub cmbCategorie_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbCategorie.SelectedIndexChanged
        isCmbCatTextChanged = False
        If cmbCategorie.SelectedIndex >= 0 Then
            btnNext.Enabled = True
            btnPrevious.Enabled = True

            objEquipement = lstEquipement.Where(Function(y As equipement) y.categorie = cmbCategorie.Text)(0)

            objEquipement.mesTypes()

            'initialise type
            vider(nouvVider)

            For Each typeEqui In objEquipement.types
                cmbType.Items.Add(typeEqui.libelle)
            Next
        Else
            btnNext.Enabled = False
            btnPrevious.Enabled = False
        End If
    End Sub
    Private Sub cmbType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbType.SelectedIndexChanged
        If cmbType.SelectedIndex >= 0 Then
            If Not isBtnSens Then
                objTypEquip = objEquipement.types.Where(Function(y As typeEquipement) y.libelle = cmbType.Text)(0)
                position = objEquipement.types.IndexOf(objTypEquip) - 1
                btnNext_Click(sender, e)
            Else
                isBtnSens = False
            End If
        End If
    End Sub
    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click

        isBtnSens = True
        position += 1
        If position >= objEquipement.types.Count Then
            position = 0
        End If
        objTypEquip = objEquipement.types(position)
        If Not objTypEquip Is Nothing Then
            txt_prix.Text = objTypEquip.prixLoc
            txtQuantite.Text = objTypEquip.quantStock
            txt_remarq.Text = objTypEquip.remarque
            Pic_photo.Image = convertByteToImage(objTypEquip.Photo)
            If cmbType.SelectedItem = objTypEquip.libelle Then
                isBtnSens = False
            End If
            cmbType.SelectedItem = objTypEquip.libelle
        End If
    End Sub
    Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click

        isBtnSens = True
        position -= 1
        If position < 0 Then
            position = objEquipement.types.Count - 1
        End If
        objTypEquip = objEquipement.types(position)
        If Not objTypEquip Is Nothing Then
            txt_prix.Text = objTypEquip.prixLoc
            txtQuantite.Text = objTypEquip.quantStock
            txt_remarq.Text = objTypEquip.remarque
            Pic_photo.Image = convertByteToImage(objTypEquip.Photo)
            cmbType.SelectedItem = objTypEquip.libelle
        End If
    End Sub
    Dim isCmbCatTextChanged As Boolean = False
    Private Sub cmbCategorie_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbCategorie.TextChanged
        isCmbCatTextChanged = True
    End Sub
End Class